package com.vf.admin.miaodong.controller;

import com.vf.admin.common.BaseController;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppGiftNum;
import com.vf.admin.miaodong.service.AfterGiftNumService;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 礼物数量管理
 * @author anxiang
 * @version 1.0.0
 * @ClassName AfterGiftNumController.java
 * @Description TODO
 * @createTime 2021年07月23日 11:40:00
 */
@CrossOrigin
@Controller
@RequestMapping("/after/giftnum")
public class AfterGiftNumController  extends BaseController{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    AfterGiftNumService afterGiftNumService;


    /**
    * 礼物数量列表
    * @Param: [splitPageBean 分页，state 显示状态（1，显示，2，不显示） ,startDate 开始时间  endDate  结束时间]
    * @return: java.lang.String
    * @Author: zhouyunyi
    * @Date: 2021/07/23
    * @Time: 11:44
    */
    @RequestMapping("/list")
    public String list(SplitPageBean splitPageBean,Integer state,String startDate,String endDate){
        try{
            Map<String,Object> map = new HashMap<>();
            map.put("state",state);
            map.put("startDate",startDate);
            map.put("endDate",endDate);
            List<AppGiftNum> list = afterGiftNumService.findList(map,splitPageBean);

            request.setAttribute("list",list);
            request.setAttribute("splitPage",splitPageBean);
            request.setAttribute("state",state);
            request.setAttribute("startDate",startDate);
            request.setAttribute("endDate",endDate);
            //显示状态
            Map<Integer,String> stateMap = Constants.getShiFouStateMap();
            request.setAttribute("stateMap",stateMap);
            return this.sucess("/web/after/giftnum/list");
        }catch (Exception e){
            logger.error("礼物数量列表异常",e);
            return this.error("系统繁忙");
        }
    }


    /**
    * 详情
    * @Param: [id]
    * @return: java.lang.String
    * @Author: zhouyunyi
    * @Date: 2021/07/23
    * @Time: 11:52
    */
    @RequestMapping("/detail")
    public String detail(Integer id){
        try{
            AppGiftNum giftNum = afterGiftNumService.get(id);
            request.setAttribute("giftnum",giftNum);
            //显示状态
            Map<Integer,String> stateMap = Constants.getShiFouStateMap();
            request.setAttribute("stateMap",stateMap);
            return this.sucess("/web/after/giftnum/edit");
        }catch (Exception e){
            logger.error("查看礼物数量详情异常",e);
            return this.error("系统繁忙");
        }
    }


    /**
    * 编辑礼物数量信息（新增/修改）
    * @Param: [appGiftNum]
    * @return: com.vf.admin.common.entity.Result
    * @Author: zhouyunyi
    * @Date: 2021/07/23
    * @Time: 11:53
    */
    @RequestMapping("/save")
    @ResponseBody
    public Result save(AppGiftNum appGiftNum){
        try{
            if(StringEx.isNull(appGiftNum.getNum())){
                return this.errorJSON("数量不能为空");
            }
            if(StringEx.isNull(appGiftNum.getName())){
                return this.errorJSON("名称不能为空");
            }

            if(StringEx.isNull(appGiftNum.getId())){
                //新增
                appGiftNum.setCreatetime(new Date());
                if(StringEx.isNull(appGiftNum.getState())){
                    appGiftNum.setState(2);//不启用
                }else{
                    appGiftNum.setState(appGiftNum.getState());
                }
                afterGiftNumService.save(appGiftNum);
            }else{
                //修改
                AppGiftNum giftNum = afterGiftNumService.get(appGiftNum.getId());
                giftNum.setName(appGiftNum.getName());
                giftNum.setNum(appGiftNum.getNum());
                if(StringEx.isNull(appGiftNum.getState())){
                    giftNum.setState(2);//不显示
                }else{
                    giftNum.setState(appGiftNum.getState());
                }
                afterGiftNumService.save(giftNum);
            }
            return this.sucessJSON("保存成功");
        }catch (Exception e){
            logger.error("编辑礼物数量信息异常",e);
            return this.errorJSON("系统繁忙");
        }
    }

    /**
    * 删除
    * @Param: [id]
    * @return: com.vf.admin.common.entity.Result
    * @Author: zhouyunyi
    * @Date: 2021/07/23
    * @Time: 12:32
    */
    @RequestMapping("/del")
    @ResponseBody
    public Result del(Integer id){
        try{
            if(StringEx.isNull(id)){
                return this.errorJSON("未获取到要删除的信息");
            }
            afterGiftNumService.del(id);
            return this.sucessJSON("删除成功");
        }catch (Exception e){
            logger.error("删除礼物数量时异常",e);
            return this.errorJSON("系统繁忙");
        }
    }

}
